﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class UpgradeRecordDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            UpgradeService upService = new UpgradeService();
            UpgradeForm upForm = upService.GetUpgradeFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
            if (!upForm.ApplicationProductReference.IsLoaded)
            {
                upForm.ApplicationProductReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationForm.UserReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationForm.UserReference.Load();
            }
            if (!upForm.UserReference.IsLoaded)
            {
                upForm.UserReference.Load();
            }
            if (!upForm.User.ServiceStationReference.IsLoaded)
            {
                upForm.User.ServiceStationReference.Load();
            }

            rmaCodeLabel.Text = upForm.ApplicationProduct.ApplicationForm.ApplicationCode;
            runningNumberLabel.Text = upForm.Code;

            // user information
            clientNameLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.FullName;
            connectPersonLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.ContactPerson;
            phoneNumberLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.UserFax;
            addressLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.UserAddress;
            emailLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.UserEmail;
            registerLabel.Text = upForm.ApplicationProduct.ApplicationForm.User.RegisterTime.HasValue ? upForm.ApplicationProduct.ApplicationForm.User.RegisterTime.Value.ToString("yyyy-MM-dd") : "";

            // service station information
            serviceStationNameLabel.Text = upForm.User.ServiceStation.Name;
            serviceStationConnectPersonLabel.Text = upForm.User.ContactPerson;
            serviceStationPhoneLabel.Text = upForm.User.UserPhone;
            serviceStationMobileLabel.Text = upForm.User.UserMobilePhone;
            serviceStationEmailLabel.Text = upForm.User.UserEmail;

            // time information
            applyLabel.Text = upForm.ApplicationProduct.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            receiveLabel.Text = upForm.ApplicationProduct.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd");
            distributeLabel.Text = upForm.ApplicationProduct.ApplicationForm.DistributeDateTime.Value.ToString("yyyy-MM-dd");
            upgradeApplyLabel.Text = upForm.ApplyTime.Value.ToString("yyyy-MM-dd");
            upgradeApproveLabel.Text = upForm.DealWithTime.HasValue ? upForm.DealWithTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            upgradeSendLabel.Text = upForm.SendTime.HasValue ? upForm.SendTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            upgradeReceiveLabel.Text = upForm.ReceiveTime.HasValue ? upForm.ReceiveTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            confirmQuatationTimeLabel.Text = upForm.ApplicationProduct.ConfirmQuotationDateTime.HasValue ? upForm.ApplicationProduct.ConfirmQuotationDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            //confirmGatheringLabel.Text = upForm.ApplicationProduct.ApplicationForm.con.HasValue ? upForm.ReceiveReturnTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            completeLabel.Text = upForm.ApplicationProduct.ApplicationForm.CompleteDateTime.HasValue ? upForm.ApplicationProduct.ApplicationForm.CompleteDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            serviceStationReceiveDateLabel.Text = upForm.CompleteTime.HasValue ? upForm.CompleteTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            clientReceiveDateLabel.Text = upForm.ApplicationProduct.ClientReceiveDateTime.HasValue ? upForm.ApplicationProduct.ClientReceiveDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            finishDateLabel.Text = upForm.ApplicationProduct.ApplicationForm.CompleteDateTime.HasValue ? upForm.ApplicationProduct.ApplicationForm.CompleteDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";

            //original application
            ApplicationProductService appServie = new ApplicationProductService();
            ApplicationProductHistory appHistory = appServie.GetProductHistoryByID(upForm.ApplicationProduct.ApplicationProductID);
            productTypeLabel.Text = appHistory.ProductType.ProductTypeName;
            productSerialNumberLabel.Text = appHistory.ProductSerialNumber;
            problemTypeLabel.Text = appHistory.ProblemType.Name;
            problemDescriptionLabel.Text = appHistory.ProductProblemDescription;
            productAppearenceLabel.Text = appHistory.ProductAppearance;
            buyDateLabel.Text = appHistory.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            repairTypeLabel.Text = appHistory.ProductRepairType;
            if (!upForm.ApplicationProduct.QuatationForm.IsLoaded)
            {
                upForm.ApplicationProduct.QuatationForm.Load();
            }
            double cost = 0;
            double freeCost = 0;
            string reason = string.Empty;
            string accessory = string.Empty;
            string costString = string.Empty;
            string quotationString = string.Empty;
            foreach (QuatationForm quotationForm in upForm.ApplicationProduct.QuatationForm)
            {
                if (quotationForm.IsBWBA.HasValue && quotationForm.IsBWBA.Value)
                {
                    cost += quotationForm.Cost;
                }
                else
                {
                    freeCost += quotationForm.Cost;
                }
                reason += "," + quotationForm.Reason;
                accessory += "," + quotationForm.AccessoryName;
                costString += "," + quotationForm.Cost.ToString();
                quotationString += "," + quotationForm.Reason;
            }
            quatationLabel.Text = cost.ToString();
            if (!string.IsNullOrEmpty(reason))
            {
                reasonLabel.Text = reason.Substring(1);
            }
            if (upForm.ApplicationProduct.IsDecideToRepair.HasValue)
            {
                if (upForm.ApplicationProduct.IsDecideToRepair == true)
                {
                    isDecideToRepairLabel.Text = "是";
                }
                else
                {
                    isDecideToRepairLabel.Text = "否";
                }
            }

            // upgrade application information
            upgradeServiceStationNameLabel.Text = upForm.User.ServiceStation.Name;
            upgardeInformationProductTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            upgradeInfoResultLabel.Text = upForm.ApplicationProduct.ProductAppearance;
            upgradeReasonLabel.Text = upForm.UpgradeReason;

            // maintain information
            maintainProductTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            repairAccessoryLabel.Text = upForm.ApplicationProduct.RepairAccessory;
            appearanceLabel.Text = upForm.ApplicationProduct.ProductAppearance;
            reasonOfProblemLabel.Text = upForm.ApplicationProduct.ReasonToProblem;
            repairPlaceLabel.Text = upForm.ApplicationProduct.AccessoryPlace;
            changeTypeLabel.Text = upForm.ApplicationProduct.ChangeType;
            replaceSerialNumberLabel.Text = upForm.ApplicationProduct.ReplaceProductSerialNumber;
            if (!upForm.ApplicationProduct.ProblemAnalyzeReference.IsLoaded)
            {
                upForm.ApplicationProduct.ProblemAnalyzeReference.Load();
            }
            problemAnalyzeLabel.Text = GetProblemAnalyze(upForm.ApplicationProduct.ProblemAnalyze);
            resultLabel.Text = upForm.ApplicationProduct.ProductProblemDescription;
            if (!upForm.ApplicationProduct.UserReference.IsLoaded)
            {
                upForm.ApplicationProduct.UserReference.Load();
            }
            if (upForm.ApplicationProduct.User != null)
            {
                engineerLabel.Text = upForm.ApplicationProduct.User.UserName;
            }
            //gather information
            gatheringProductTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            gatheringProductSerialLabel.Text = upForm.ApplicationProduct.ProductSerialNumber;
            if (!string.IsNullOrEmpty(accessory))
            {
                gatheringaccessoryLabel.Text = accessory.Substring(1);
            }
            if (!string.IsNullOrEmpty(costString))
            {
                gatheringcostLabel.Text = costString.Substring(1);
            }
            if (!string.IsNullOrEmpty(quotationString))
            {
                gatheringReasonLabel.Text = quotationString.Substring(1);
            }
            totalCostLabel.Text = cost.ToString();

            if (!upForm.ApplicationProduct.ApplicationForm.GatheringReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationForm.GatheringReference.Load();
            }

            if (upForm.ApplicationProduct.ApplicationForm.Gathering != null)
            {
                if (upForm.ApplicationProduct.ApplicationForm.Gathering.RealPay.HasValue)
                {
                    realPayLabel.Text = upForm.ApplicationProduct.ApplicationForm.Gathering.RealPay.ToString();
                }

                costForFreeLabel.Text = freeCost.ToString();
                freeReasonLabel.Text = "保外备案";
                gatheringRemarkLabel.Text = upForm.ApplicationProduct.ApplicationForm.Gathering.Remark;
            }

            // requisition information
            if (!upForm.ApplicationProduct.RequisitionFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.RequisitionFormReference.Load();
            }
            Label14.Text = upForm.ApplicationProduct.SendWay;
            if (upForm.ApplicationProduct.RequisitionForm != null)
            {
                if (!upForm.ApplicationProduct.RequisitionForm.LogisticCompanyReference.IsLoaded)
                {
                    upForm.ApplicationProduct.RequisitionForm.LogisticCompanyReference.Load();
                }
                Label13.Text = upForm.ApplicationProduct.RequisitionForm.DistributionFormID;
                Label15.Text = upForm.ApplicationProduct.RequisitionForm.WayOfTransport;
                Label16.Text = upForm.ApplicationProduct.RequisitionForm.LogisticCompany.Name;
                Label17.Text = upForm.ApplicationProduct.RequisitionForm.RequisitionPerson;
            }

            //client comment
            if (!upForm.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!upForm.ApplicationProduct.ApplicationForm.ConfirmReceiveProduct.IsLoaded)
            {
                upForm.ApplicationProduct.ApplicationForm.ConfirmReceiveProduct.Load();
            }
            clientCommentConfirmGatheringLabel.Text = upForm.ApplicationProduct.ClientReceiveDateTime.HasValue ? "是" : "否";
            if (upForm.ApplicationProduct.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault() != null)
            {
                clientCommentSuggestingLabel.Text = upForm.ApplicationProduct.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault().Comment;
                clientCommentConfirmWayLabel.Text = upForm.ApplicationProduct.ApplicationForm.ConfirmReceiveProduct.FirstOrDefault().ConfirmWay;
            }
            //maintainQualityLabel.Text = 
        }

        private string GetProblemAnalyze(ProblemAnalyze problemAnalyze)
        {
            string result = string.Empty;
            if (problemAnalyze != null)
            {
                if (!string.IsNullOrEmpty(problemAnalyze.A))
                {
                    result += "," + problemAnalyze.A;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.B))
                {
                    result += "," + problemAnalyze.B;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.C))
                {
                    result += "," + problemAnalyze.C;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.D))
                {
                    result += "," + problemAnalyze.D;
                }
                if (!string.IsNullOrEmpty(problemAnalyze.E))
                {
                    result += "," + problemAnalyze.E;
                }
            }
            if (!string.IsNullOrEmpty(result))
            {
                result = result.Substring(1);
            }
            return result;
        }
    }
}
